/#!/bin/bash



path_output='..'
path_main='/home/ubuntu/projettp1'
filename_output='picturesData.sql';
table_pictures='multimedia2'
lineno=0
table_fields=( "extension" "color" "bordernbpixel" "meanGradient" "bhattacharyya" "imnbpixel" "imWidth"  "imHight" "red" "green" "blue")

#als ../images`
echo start processing pictures

function tosql {
        echo "$1" >> "$path_output/$filename_output"
}
function tosql_init {
	touch "$path_output/$filename_output"
}


#Creation du fichier PL/SQL
echo -e "" > $path_output/$filename_output
tosql "--Projet Indexation d Image - Universite de Cergy-Pontoise"
tosql "--fichier PL/SQL d import des donnees images et associees"


##########
#DECLARE PL/SQL
##########

tosql "declare"
tosql "c int;"


##########
#BEGIN
##########
tosql "begin"

tosql "select count(1) into c from user_tables where table_name = upper('$table_pictures');"
tosql "if c = 1 then"
tosql "execute immediate 'drop table $table_pictures';"
tosql "end if;"

tosql "execute immediate 'create table multimedia2("
tosql "nom varchar2(50),"
tosql "extension varchar2(10),"
tosql "color number,"
tosql "bordernbpixel number,"
tosql "meanGradient number,"
tosql "bhattacharyya  number,"
tosql "imnbpixel  number,"
tosql "imWidth  number,"
tosql "imHight  number,"
tosql "red  number,"
tosql "green  number,"
tosql "blue  number"
tosql ")';"













#Generation des requetes SQL pour chaque image disponible dans le dossier /image:
for image in `ls ../images | egrep -e ".pgm$"`
do
	touch "../images/$image.txt"
	echo "fichier: $image" #echo stdout
	tosql "execute immediate 'insert into multimedia2(nom) values (''$image'')';"
#	tosql "execute immediate 'update multimedia2 set nom=''$image'' where nom=''$image''';"
	tosql "" 












# TRAITEMENT IMAGE

#0
EXTENSION=`echo "$image" | cut -d'.' -f2`
cp ../bin/* ../images/; cd ../images/;


echo $EXTENSION > $image.txt
#1./rgb8_or_not &>> log #couleur pour fichier ppm
echo "0" >> $image.txt
#2
./AffichPixBlanc $image &>> log

#3
./Sobel $image &>> log

#4 #battaNONNN
echo "0" >> $image.txt

#5
#totalpix ...
./AffichPixBlanc $image &>> log
#6
./AffichLargeur $image &>> log

#7
./AffichHateur $image &>> log

#8,9,10
#Rouge
#Bleu
#Vert
#./RGB_ppm $image &>> log #couleur pour fichier ppm
echo -1 >> $image.txt
echo -1 >> $image.txt
echo -1 >> $image.txt














# INSERTION EN BASE

	for im_data in `cat "../images/$image.txt"`
	do
		#echo "$image $lineno: $im_data"
		#echo $table_fields[$lineno]
		tosql "execute immediate 'update multimedia2 set ${table_fields[$lineno]}=''$im_data'' where nom=''$image''';"
		lineno=`expr $lineno + 1`
	done
	lineno=0
rm ../images/${image}_*.pgm &> log2 >> log2
rm ../images/${image}_*.txt &> log2 >> log2
echo "rm ../images/${image}_*.pgm"
done


#########
#END
########

tosql "end;"


